package gw.com.android.ui.quote2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigTypesDeal;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemResult;


/**
 * 行情页 产品展示
 * ViewPager+TabLayout的组合，数据看来全都来自这里,包括头部和内容的数据。
 */
public class ProductTypeAdapter extends FragmentPagerAdapter {

    private List<String[]> titleList;

    private DataItemResult mTabTitles;
    private ConfigTypesDeal mTypesDeal;


    /**
     * CPS开关如果打开，就读取6个分类，如果关闭，就少一个数字货币;
     * 可是如果是游客，直接只加载自选，其他的一律没有
     *
     * @param fm
     * @param ifGuest
     * @param ifCpsOpen
     */
    public ProductTypeAdapter(FragmentManager fm, boolean ifGuest, boolean ifCpsOpen) {
        super(fm);
        mTypesDeal = new ConfigTypesDeal();
        //不再区分游客
        if (ifCpsOpen) {
            mTabTitles = mTypesDeal.getQuoteTypeList();//那就取全部类型
        } else {
            mTabTitles = mTypesDeal.getQuoteTypeListWithoutBtType();
        }

        //这样，解析配置读出来的产品类型
        titleList = new ArrayList<>();
        titleList.add(new String[]{"自选", GTSConst.ZONE_TYPE_SELF + ""});
        if (mTabTitles != null)
            for (int i = 0; i < mTabTitles.getDataCount(); i++) {
                String titleId = mTabTitles.getItem(i).getString(ConfigType.CONFIG_TYPE_TYPE_TAG);//类型id
                String titleName = mTabTitles.getItem(i).getString(ConfigType.CONFIG_TYPE_TITLE_TAG);//类型名字
                titleList.add(new String[]{titleName, titleId});
            }

    }

    @Override
    public Fragment getItem(int position) {
        QuoteInsideFragment fragment = QuoteInsideFragment.newInstance();
        Bundle bundle = new Bundle();
        bundle.putString(QuoteInsideFragment.CODE_ID_TAG, titleList.get(position)[1]);
        bundle.putString(QuoteInsideFragment.NAME_TAG, titleList.get(position)[0]);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getCount() {
        return titleList == null ? 0 : titleList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position)[0];
    }


    /**
     * 重写此方法,防止滑动时产生卡顿现象
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}
